package nl.saxion.act.entity;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "Orders")
public class Order implements Serializable{
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long id;
	
	private char isOrdered;
	
	@ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name="CUST_ID", nullable = false)
	private Customer customer;
	
	@OneToMany(cascade = CascadeType.PERSIST)
	@JoinTable
	(
		name="Order_Product_Mapping",
	    joinColumns={ @JoinColumn(name="ORDER_ID", referencedColumnName="ID") },
	    inverseJoinColumns={ @JoinColumn(name="PRODUCT_ID", referencedColumnName="ID") }
	)
	private List<Product> products;
		
	public Order() {
		products = new LinkedList<Product>();
	}
	
	// Getters & Setters
	public boolean getIsOrdered() {
		return isOrdered == 'Y';
	}

	public void setIsOrdered( boolean isOrdered ) {
		this.isOrdered = (isOrdered ? 'Y' : 'N');
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer( Customer customer ) {
		this.customer = customer;
	}
	
	public List<Product> getProducts() {
		return products;
	}

//	public void setProducts( List<Product> products ) {
//		this.products = products;
//	}
	
	// You have to manage the Java relationships yourself.
	public void addProduct( Product product ) {
		products.add( product );
	}

	public long getId() {
		return id;
	}

}
